const request = require("../utils/request");
const { getFormatError } = require("../utils");

const UserService = {
  // 获取电影列表
  getMovieList: async (data) => {
    let res;
    try {
      res = await request({
        url: "https://m.douban.com/rexxar/api/v2/movie/recommend",
        method: "GET",
        data,
        headers: {
          referer: "https://movie.douban.com/tv/",
        },
      });
    } catch (err) {
      throw getFormatError(err);
    }
    return res?.data;
  },
  // 获取电视剧列表
  getDramaList: async (data) => {
    let res;
    try {
      if (data.platform === "tencentTv") {
        res = await request({
          url: "https://pbaccess.video.qq.com/trpc.multi_vector_layout.mvl_controller.MVLPageHTTPService/getMVLPage?vplatform=2",
          method: "POST",
          data,
          headers: {
            origin: "https://v.qq.com",
            referer: "https://v.qq.com",
          },
        });
        if (!res?.data?.data) {
          const error = new Error(
            res?.data?.msg || res?.data?.message || "请求失败",
          );
          error.status = 400;
          error.data = res?.data;
          throw error;
        }
        const start =
          +data?.page_context?._ds_cli_6970df954e7a9803_poster_offset;
        const count = +data?.page_context?._ds_cli_6970df954e7a9803_poster_size;
        res = {
          start: Number.isNaN(start) ? null : start,
          count: Number.isNaN(count) ? null : count,
          total: null,
          items:
            res?.data?.data?.modules?.normal?.cards?.[0]?.children_list?.poster_card?.cards?.map(
              (item) => {
                return item?.params;
              },
            ),
        };
      } else if (data.platform === "iqiyi") {
        res = await request({
          url: "https://mesh.if.iqiyi.com/portal/lw/videolib/data",
          method: "GET",
          data,
        });
        if (!res?.data?.data) {
          const error = new Error(
            res?.data?.msg || res?.data?.message || "请求失败",
          );
          error.status = 400;
          error.data = res?.data;
          throw error;
        }
        const start = +data?.page_id;
        res = {
          start: Number.isNaN(start) ? null : start,
          count: null,
          total: null,
          items: res?.data?.data,
        };
      } else {
        res = await request({
          url: "https://m.douban.com/rexxar/api/v2/tv/recommend",
          method: "GET",
          data,
          headers: {
            referer: "https://movie.douban.com/tv/",
          },
        });
        if (!res?.data) {
          const error = new Error(
            res?.data?.msg || res?.data?.message || "请求失败",
          );
          error.status = 400;
          error.data = res?.data;
          throw error;
        }
        res = res?.data;
      }
    } catch (err) {
      throw getFormatError(err);
    }
    return res;
  },
  // 获取媒体详情
  getMediaDetail: async (id) => {
    let res;
    try {
      res = await request({
        url: `https://movie.douban.com/subject/${id}/`,
        method: "GET",
        headers: {
          referer: "https://movie.douban.com/tv/",
        },
      });
    } catch (err) {
      throw getFormatError(err);
    }
    return res?.data;
  },
};

module.exports = UserService;
